#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>


#define ADDRESS     "ssl://brambling-device.chinafsl.com:21005"

#define CLIENTID    "0ft1sbe6856j39ef"
#define USERNAME    "PLCGW-0ft1sbe"
#define PASSWORD    "p40UPQZ36M5s9275"

#define TOPIC       "test_topic"
#define PAYLOAD     "Hello MQTT!"

#define QOS         1
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;

    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    
    conn_opts.ssl= &ssl_opts;
    conn_opts.username = USERNAME;
    conn_opts.password = PASSWORD;
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;    // 设置连接选项中的清除会话标志


    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect to MQTT server, return code %d\n", rc);
        exit(-1);
    }

    printf("Connected to MQTT server\n");

    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = (int)strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
    printf("Message published\n");

    MQTTClient_disconnect(client, TIMEOUT);
    MQTTClient_destroy(&client);

    return 0;
}

